package com.xteam.youin_supplier.adpter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.fragment.StationDetailFragment_;
import com.xteam.youin_supplier.fragment.TruckTeamListFragment_;
import com.xteam.youin_supplier.model.OnlineStation;


/**
 * Created by Curry on 2015-12-16.
 */
public class OnlineStationListViewAdapter extends RecyclerView.Adapter<OnlineStationListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Fragment mContext;
    private OnlineStation onlineStation;
    private String type = "0";//0:本日 1：本周 2：本月

    public OnlineStationListViewAdapter(Fragment fragment, OnlineStation onlineStation, String type) {
        mContext = fragment;
        this.onlineStation = onlineStation;
        this.type = type;
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public OnlineStationListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.online_station_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OnlineStationListViewAdapter.ViewHolder holder, final int position) {
        holder.itemView.setTag(position);

        holder.num.setText(onlineStation.getData().getList().get(position).getQty());
        holder.stationName.setText(onlineStation.getData().getList().get(position).getName());
        holder.num.setTag(position);
        holder.num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.getInstance().switchFragment(mContext, TruckTeamListFragment_.builder()
                        .time(type)
                        .gasstation_id(onlineStation.getData().getList().get(position).getGasstation_id())
                        .build());
//                Log.i("curry", "position:"+position);
            }
        });
        holder.stationName.setTag(position);
        holder.stationName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.getInstance().switchFragment(mContext, StationDetailFragment_.builder()
                        .time(type)
                        .gasstation_id(onlineStation.getData().getList().get(position).getGasstation_id())
                        .build());
            }
        });
        SwitchBg(holder, position);
    }

    @Override
    public int getItemCount() {
        return onlineStation.getData().getList().size();
    }


    private void SwitchBg(OnlineStationListViewAdapter.ViewHolder holder, int position) {
        switch (position % 4) {
            case 0:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_blue_bg));
                break;
            case 1:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_orage_bg));
                break;
            case 2:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_red_bg));
                break;
            case 3:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_gray_bg));
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout layout;
        private TextView stationName;
        private TextView num;

        public ViewHolder(View itemView) {
            super(itemView);
            layout = (RelativeLayout) itemView.findViewById(R.id.layout);
            stationName = (TextView) itemView.findViewById(R.id.station_name);
            num = (TextView) itemView.findViewById(R.id.num);
        }

        public RelativeLayout getLayout() {
            return layout;
        }

        public void setLayout(RelativeLayout layout) {
            this.layout = layout;
        }

        public TextView getStationName() {
            return stationName;
        }

        public void setStationName(TextView stationName) {
            this.stationName = stationName;
        }

        public TextView getNum() {
            return num;
        }

        public void setNum(TextView num) {
            this.num = num;
        }
    }
}
